# -*- coding: utf-8 -*-
"""
@Time ： 2023/5/9 0009 10:07
@Auth ： zpt
@File ：CustomImage.py
@IDE ：PyCharm
"""
from selenium.webdriver.common.by import By
from Page.LoginPage import LoginPage
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from Lib.common_lib import LOG_INFO, LOG_ERROR
import time


class CustomImage(object):
    """镜像定制页面"""
    group_name = None
    PipelineName = None
    pipeline_type_name = None
    format = None

    def __init__(self, driver):
        self.driver = driver

    def get_url(self, url):
        self.driver.get(url)

    def user_login(self, user_name, password):
        """
        用户登录
        """
        login = LoginPage(self.driver)
        login.user_login()
        login.login_valid(user_name, password)

    def add_pipeline(self):
        """
        添加流水线按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[1]/div/div[1]/button').click()
        except Exception as e:
            message = '添加流水线按钮元素定位失败'
            LOG_ERROR("error: found add_pipeline timeout")
            print(e)
            LOG_INFO(message)

    def div_cancel(self):
        """
        弹窗取消按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[2]/div/div/footer/div/button[2]'
                                               '/span').click()
        except Exception as e:
            message = '弹窗取消按钮元素定位失败'
            LOG_ERROR("error: found div_cancel timeout")
            print(e)
            LOG_INFO(message)

    def div_close(self):
        """
        弹窗关闭按钮
        """
        try:
            self.driver.find_element(By.XPATH,
                                     '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/header/button').click()
        except Exception as e:
            message = '弹窗元素关闭按钮定位失败'
            LOG_ERROR("error: found div_close timeout")
            print(e)
            LOG_INFO(message)

    def div_confirm(self):
        """
        弹窗确认按钮
        """
        try:
            self.driver.find_element(By.XPATH, '//*[@id="app"]/div[1]/div/div[2]/div[2]/div/div/footer/div/button[1]'
                                               '/span').click()
        except Exception as e:
            message = '弹窗取消按钮元素定位失败'
            LOG_ERROR("error: found div_confirm timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_type(self):
        """
        流水线类型
        """
        try:
            self.pipeline_type_name = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                                         '2]/div/div/div/form/div['
                                                                         '1]/div/div/div/div/div/input')
        except Exception as e:
            message = '添加流水线类型下拉框定位失败'
            LOG_ERROR("error: found pipeline_type timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_scene(self):
        """
        流水线场景-仅镜像定制
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div['
                                               '2]/div/div/div/div/div/input').click()
        except Exception as e:
            message = '添加流水线格式输入框定位失败'
            LOG_ERROR("error: found pipeline_scene timeout")
            print(e)
            LOG_INFO(message)

    def switch_pipeline_type(self, build_type="vm"):
        """
        选择镜像类型
        """
        if build_type == "vm":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[9]/div/div/div[1]/ul/li[1]').click()
            except Exception as e:
                message = '版本镜像定位失败'
                LOG_ERROR("error: found switch_pipeline_version_image timeout")
                print(e)
                LOG_INFO(message)
        else:
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[9]/div/div/div[1]/ul/li[2]').click()
            except Exception as e:
                message = '镜像定制定位失败'
                LOG_ERROR("error: found switch_pipeline_image_build timeout")
                print(e)
                LOG_INFO(message)

    def switch_pipeline_format(self, build_format="iso"):
        """
        选择镜像格式
        """
        if build_format == "iso":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[12]/div/div/div[1]/ul/li[1]').click()
            except Exception as e:
                message = 'iso格式定位失败'
                LOG_ERROR("error: found switch_pipeline_iso timeout")
                print(e)
                LOG_INFO(message)
        elif build_format == "docker":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[12]/div/div/div[1]/ul/li[2]').click()
            except Exception as e:
                message = 'docker格式定位失败'
                LOG_ERROR("error: found switch_pipeline_docker timeout")
                print(e)
                LOG_INFO(message)
        elif build_format == "stratovirt":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[12]/div/div/div[1]/ul/li[3]').click()
            except Exception as e:
                message = 'stratovirt格式定位失败'
                LOG_ERROR("error: found switch_pipeline_stratovirt timeout")
                print(e)
                LOG_INFO(message)
        elif build_format == "service_iso" or build_format == "cpio":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[13]/div/div/div[1]/ul/li[1]').click()
            except Exception as e:
                message = 'service_iso格式定位失败'
                LOG_ERROR("error: found switch_pipeline_service_iso timeout")
                print(e)
                LOG_INFO(message)
        elif build_format == "service_docker" or build_format == "mini_cpio":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[13]/div/div/div[1]/ul/li[2]').click()
            except Exception as e:
                message = 'service_iso格式定位失败'
                LOG_ERROR("error: found switch_pipeline_service_iso timeout")
                print(e)
                LOG_INFO(message)
        elif build_format == "mini_docker":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[13]/div/div/div[1]/ul/li[3]').click()
            except Exception as e:
                message = 'mini_docker格式定位失败'
                LOG_ERROR("error: found switch_pipeline_mini_docker timeout")
                print(e)
                LOG_INFO(message)
        elif build_format == "iso_normal":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[13]/div/div/div[1]/ul/li[4]').click()
            except Exception as e:
                message = 'iso_normal格式定位失败'
                LOG_ERROR("error: found switch_pipeline_iso_normal timeout")
                print(e)
                LOG_INFO(message)
        elif build_format == "qcow2":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[13]/div/div/div[1]/ul/li[5]').click()
            except Exception as e:
                message = 'qcow2格式定位失败'
                LOG_ERROR("error: found switch_pipeline_qcow2 timeout")
                print(e)
                LOG_INFO(message)
        else:
            LOG_ERROR("Error image format")

    def switch_pipeline_arch(self, build_arch="arm"):
        """
        选择镜像架构
        """
        if build_arch == "arm":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[11]/div/div/div[1]/ul/li[1]').click()
            except Exception as e:
                message = 'arm格式定位失败'
                LOG_ERROR("error: found switch_pipeline_arm timeout")
                print(e)
                LOG_INFO(message)
        else:
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[11]/div/div/div[1]/ul/li[2]').click()
            except Exception as e:
                message = 'x86格式定位失败'
                LOG_ERROR("error: found switch_pipeline_x86 timeout")
                print(e)
                LOG_INFO(message)

    def switch_pipeline_scene(self, build_scene="service"):
        """
        选择镜像场景
        """
        if build_scene == "service":
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[13]/div/div/div[1]/ul/li[1]').click()
            except Exception as e:
                message = '服务器镜像场景定位失败'
                LOG_ERROR("error: found switch_pipeline_service timeout")
                print(e)
                LOG_INFO(message)
        else:
            try:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[13]/div/div/div[1]/ul/li[2]').click()
            except Exception as e:
                message = '嵌入式镜像场景定位失败'
                LOG_ERROR("error: found switch_pipeline_embedding timeout")
                print(e)
                LOG_INFO(message)

    def pipeline_send_name(self, group_name, pipeline_name):
        """
        添加流水线分组名和流水线名称
        """
        try:
            # 输入流水线名称
            self.PipelineName.send_keys(pipeline_name)
            # 输入流水线分组名
            self.group_name.send_keys(group_name)
            time.sleep(1)
            if "image_build" in group_name:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[10]/div/div/div[1]/ul/li[12]').click()
            else:
                self.driver.find_element(By.XPATH, '/html/body/div[2]/div[10]/div/div/div[1]/ul/li[1]').click()
            # self.group_name.send_keys(Keys.ENTER)

        except Exception as e:
            message = '分组名下拉框元素定位失败'
            LOG_ERROR("error: found group_name timeout")
            print(e)
            LOG_INFO(message)

    def length_info_valid(self):
        """
        获取长度校验提示信息
        """
        warn_info_1 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                         '2]/div/div/div/form/div[2]/div/div[2]')

        warn_info_2 = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                         '2]/div/div/div/form/div[3]/div/div[2]')

        return [warn_info_1.text, warn_info_2.text]

    def alert_message(self):
        """
        获取弹窗元素
        """
        mouse_alert = self.driver.find_element(By.XPATH, '/html/body/div[3]')
        ActionChains(self.driver).move_to_element(mouse_alert).perform()
        alert = self.driver.find_element(By.XPATH, '/html/body/div[3]')

        return alert.text

    def pipeline_group(self):
        """
        流水线分组
        """
        try:
            self.group_name = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                                 '2]/div/div/div/form/div[last() - '
                                                                 '3]/div/div/div/div/div/input')
        except Exception as e:
            message = '添加流水线分组输入框定位失败'
            LOG_ERROR("error: found pipeline_group timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_name(self):
        """
        流水线名称
        """
        try:
            self.PipelineName = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                                   '2]/div/div/div/form/div[last() - 2]/div/div['
                                                                   '1]/div/input')
        except Exception as e:
            message = '添加流水线名称输入框定位失败'
            LOG_ERROR("error: found pipeline_name timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_format(self):
        """
        流水线格式
        """
        try:
            self.format = self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div['
                                                             '2]/div/div/div/form/div[last() - '
                                                             '1]/div/div/div/div/div/input')
        except Exception as e:
            message = '添加流水线格式输入框定位失败'
            LOG_ERROR("error: found pipeline_format timeout")
            print(e)
            LOG_INFO(message)

    def pipeline_arch(self):
        """
        流水线架构
        """
        try:
            self.driver.find_element(By.XPATH, '/html/body/div[1]/div[1]/div/div[2]/div[2]/div/div/div/form/div[last('
                                               ')]/div/div/div/div/div/input').click()
        except Exception as e:
            message = '添加流水线格式输入框定位失败'
            LOG_ERROR("error: found pipeline_arch timeout")
            print(e)
            LOG_INFO(message)

    def add_pipeline_image_build(self, image_type, pipeline_group, pipeline_name, image_format, arch, flag=False,
                                 image_scene=None):
        """
        添加流水线
        """
        time.sleep(6)
        self.add_pipeline()
        time.sleep(3)
        self.pipeline_type()
        self.pipeline_type_name.click()
        time.sleep(1)
        self.switch_pipeline_type(build_type=image_type)
        if flag:
            # only image_build
            self.pipeline_scene()
            time.sleep(1)
            self.switch_pipeline_scene(build_scene=image_scene)
        pipe_group = pipeline_group
        pipe_name = pipeline_name
        self.pipeline_name()
        time.sleep(1)
        self.pipeline_group()
        time.sleep(1)
        self.pipeline_send_name(pipe_group, pipe_name)
        time.sleep(1)
        self.pipeline_format()
        time.sleep(1)
        self.format.click()
        time.sleep(1)
        self.switch_pipeline_format(build_format=image_format)
        self.pipeline_arch()
        time.sleep(1)
        self.switch_pipeline_arch(build_arch=arch)
        time.sleep(1)
        self.div_confirm()
        self.driver.implicitly_wait(30)
        info = self.alert_message()

        return info
